//
//  PASetHomeView.m
//  PAMobileWallet
//
//  Created by Pay_SMP002 on 13-7-4.
//  Copyright (c) 2013年 平安付. All rights reserved.
//

#import "PASetHomeView.h"
#import "PAPersonalViewController.h"
#import "PASetHomeViewCell.h"
#import "PASetHomeViewController.h"
#import "PABaseViewController.h"

@implementation PASetHomeView
@synthesize setTableView;
@synthesize listArray;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self){
        CGSize size = frame.size;
        CGSize buttonSize = CGSizeMake(43, 43);

        UIButton * gohomeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        gohomeButton.frame = CGRectMake(size.width-buttonSize.width-10, 10 , buttonSize.width , buttonSize.height);
        [gohomeButton setBackgroundImage:[UIImage imageNamed:@"set_gohomeBT"] forState:UIControlStateNormal];
        [gohomeButton addTarget:self action:@selector(buttonActive:) forControlEvents:UIControlEventTouchUpInside];
        gohomeButton.tag = 0;
        [self addSubview:gohomeButton];
        
        NSMutableArray * textArray = [[NSMutableArray alloc] initWithObjects:@"13627689876",@"客服955333",@"打分/反馈",@"关于转账宝",@"版本更新",@"安全退出",nil];
        NSMutableArray * picArray = [[NSMutableArray alloc] initWithObjects:@"set_icon1.png",@"set_icon2.png",@"set_icon3.png",@"set_icon4.png",@"set_icon5.png",@"set_icon6.png",nil];
        listArray = textArray;
        picListArray = picArray;

        self.setTableView = [[UITableView alloc] initWithFrame:CGRectMake(5, 45 + 20, 310, 300 + 119/2) style:UITableViewStylePlain];
        self.setTableView.delegate = self;
        self.setTableView.dataSource = self;
        self.setTableView.backgroundView = nil;
        self.setTableView.backgroundColor = [UIColor clearColor];
        //setTableView.separatorStyle = UITableViewCellSeparatorStyleNone; //去掉横线
        [self addSubview:self.setTableView];
    }
    return self;
}

-(void)buttonActive:(id)sender
{
    if (self.parentController && [self.parentController respondsToSelector:@selector(gotoPersonalController:)]){
        [self.parentController performSelector:@selector(gotoPersonalController:) withObject:sender];
    }
}

#pragma mark table view

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 1;
}

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
    return 6;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 119/2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    PASetHomeViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (nil == cell)
    {
        cell = [[PASetHomeViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellAccessoryNone;
    }
    
    cell.imageView.image = [UIImage imageNamed:[picListArray objectAtIndex:indexPath.row]];
    cell.nameLable.text = [listArray objectAtIndex:indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * sendMessage = nil;
    
    if (indexPath.row != 0)
    {
        if (indexPath.row == 1)
        {
            NSURL* url = [NSURL URLWithString:@"tel://955333"];
            if([[UIApplication sharedApplication] canOpenURL:url])
            {
                [[UIApplication sharedApplication] openURL:url];
                return;
            }
            else
            {
                sendMessage = @"抱歉，当前环境无法拨号！";
            }

        }
        
        if (indexPath.row == 4)
        {
            sendMessage = @"当前是最新版本";
        }
        if (indexPath.row == 5)
        {
            sendMessage = @"确定要退出吗";
        }
        [self setAlertView:sendMessage getRow:indexPath.row];
    }
}

- (void)setAlertView:(NSString *)getMessage getRow:(NSInteger)row
{
    if (row == 2 || row == 3 )
    {
        if (self.parentController && [self.parentController respondsToSelector:@selector(sendMessage:)])
        {
            [self.parentController sendMessage:row];
        }
    }
    else
    {
        UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:getMessage delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        alertView.tag = row;
        [alertView show];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
{
    if (alertView.tag == 5 && buttonIndex == 1)
    {
        if (self.parentController && [self.parentController respondsToSelector:@selector(sendMessage:)])
        {
            [self.parentController sendMessage:5];
        }
    }
}

@end
